%::PROVIDES=(
	name	=> ["VERBOSE","LOGCYCLE"],
	desc	=> ["Dynamic logfile verbosity support","Cycle current logfile"],
	type	=> "request",
	code	=> \&::do_handle_verbose
);

sub do_handle_verbose {
my $args=shift;
my $cmdname=$args->{"_CMDNAME"};
my ($maxlogs,$n1,$n2);
my @exts=qw(gz Z bz2);

	#################################################
	# Turning the logging on or off is easy...	#
	#################################################

	if($cmdname eq "VERBOSE") {
		if(exists($args->{ON})) {
			$ARG_verbose=1;
			if(exists($args->{FORWARD})) {
				logmsg("Forwarding VERBOSE request to remote node.");
				queue_it("VERBOSE on=true");
			}
			return "OK LOGGING=ON";
		}
		if(exists($args->{OFF})) {
			$ARG_verbose=0;
			if(exists($args->{FORWARD})) {
				logmsg("Forwarding VERBOSE request to remote node.");
				queue_it("VERBOSE off=true");
			}
			return "OK LOGGING=OFF";
		}
		return "MISSING_ARG";
	}

	#################################################
	# Logcycle requires a count=x variable.		#
	#################################################

	if(!exists($args->{COUNT})) {
		return "MISSING_ARG";
	}

	$maxlogs=$args->{COUNT};
	if($maxlogs<1 || $maxlogs>99) {
		return "INVALID_ARG";
	}

	if(! -f $ARG_file) {
		return "LOGFILE_NOT_FILE ($ARG_file)";
	}

	#################################################################
	# Close stdout/stderr just in case, though should be different	#
	# files for 1.0.4 onwards.					#
	#################################################################
	close(STDOUT);
	close(STDERR);

	$maxlogs--;
	while($maxlogs) {
		$n1=sprintf("$ARG_file.%03d",$maxlogs);
		$n2=sprintf("$ARG_file.%03d",$maxlogs-1);
		if(-f $n1) {
			unlink $n1;
		}
		if(-f $n2) {
			rename $n2,$n1;
		}
		for (@exts) {
			$n1=sprintf("$ARG_file.%03d.$_",$maxlogs);
			$n2=sprintf("$ARG_file.%03d.$_",$maxlogs-1);
			if(-f $n1) {
				unlink $n1;
			}
			if(-f $n2) {
				rename $n2,$n1;
			}
		}

		$maxlogs--;
	}
	$n2=sprintf("$ARG_file.%03d",0);
	if(-f $ARG_file) {
		rename $ARG_file,$n2;
	}

	open(STDOUT,">>/var/log/cluster/cldaemon-$::CLUSTER_NAME.stdout") or die("Can not open STDOUT file for writing: $!");
	open(STDERR,">>/var/log/cluster/cldaemon-$::CLUSTER_NAME.stderr") or die("Can not open STDERR file for writing: $!");
	my $cdesc=select(STDOUT); $|=1; select($cdesc);
	$cdesc=select(STDERR); $|=1; select($cdesc);

	$n1=$ARG_verbose;
	$ARG_verbose=1;
	logmsg("Log file cycled on user request.");
	$ARG_verbose=$n1;
	if(exists($args->{FORWARD})) {
		logmsg("Forwarding LOGCYCLE request to remote node.");
		queue_it("LOGCYCLE count=".$args->{COUNT});
	}
	return "OK";
}

1;

